package com.todoclipse.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Util class for reading http responses
 * @author eximius
 *
 */
public class Reader {
	/**
	 * It simply reads http content
	 * @param url Url to read
	 * @return readed content. Can be empty string. Will never return null.
	 * @throws MalformedURLException in case of badly formed url
	 * @throws IOException in case of exception during content reading 
	 */
	public String readContent(String url) throws MalformedURLException, IOException {
		InputStream content = null;
		try{
			content = (InputStream) new URL(url).getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(content));
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine() ) != null){
				sb.append(line);
			}
			return sb.toString();
		}finally{
			if(content!=null){
				content.close();
			}
		}
	}
}
